package com.hhxy.session.servlet;

import com.hhxy.session.db.BookDB;
import com.hhxy.session.pojo.Book;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

/**
 * @author ghp
 * @date 2022/9/26
 * @Title
 * @description 展示所有的图书，给用户购买
 */
@WebServlet("/listBookServlet")
public class ListBookServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取所有的图书
        Collection<Book> books = BookDB.getAll();

        response.setContentType("text/html;charset=utf-8");
        PrintWriter pw = response.getWriter();
        pw.write("本站提供的图书有：<br>");
        for (Book book : books) {
            String url = "http://localhost:8080/day04_Fifth/purchaseServlet?id="+book.getId();
            pw.write(book.getName() + "<a href='" + url + "'>点击购买</a><br>");
        }
    }
}